package com.android.RssReader.Provider;

import com.android.RssReader.Service.ServiceManager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class NewsManagerDB {
	private Context context;
	private NewsDatabaseHelper newsDatabaseHelper;
	private SQLiteDatabase newsDatabase;

	public NewsManagerDB(Context context) {
		this.context = context;
	}

	public void open() {
		newsDatabaseHelper = new NewsDatabaseHelper(context);
		newsDatabase = newsDatabaseHelper.getWritableDatabase();
	}

	public void close() {
		newsDatabaseHelper.close();
	}

	public boolean insertCountLocalNews(ContentValues values) {
		return newsDatabase.insert(NewsDatabaseHelper.TAB_NEWS, null, values) > 0;
	}
	
	public boolean deleteCountLocalNews(String tag) {
		return newsDatabase.delete(NewsDatabaseHelper.TAB_NEWS, NewsDatabaseHelper.COLUMN_NEWS_CATEGORY + " =? ", new String[] { tag }) >= 0;
	}
	
	public boolean isInLocalNews(String id) {
		Cursor cursor = newsDatabase.query(NewsDatabaseHelper.TAB_NEWS, new String[] { NewsDatabaseHelper.COLUMN_NEWS_ID },
				NewsDatabaseHelper.COLUMN_NEWS_ID + " =? ",  new String[] { id }, null, null, null);
		boolean result = false;
		if (cursor.getCount() > 0) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	
	public boolean isInLocalFavoritesNews(String id) {
		Cursor cursor = newsDatabase.query(NewsDatabaseHelper.TAB_FAVORITES_NEWS, new String[] { NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_ID },
				NewsDatabaseHelper.COLUMN_FAVORITES_NEWS_ID + " =? ",  new String[] { id }, null, null, null);
		boolean result = false;
		if (cursor.getCount() > 0) {
			result = true;
		}
		cursor.close();
		return result;
	}
	

	
	public Cursor queryCountLocalNews(String tag) {
		return newsDatabase.query(NewsDatabaseHelper.TAB_NEWS, null, NewsDatabaseHelper.COLUMN_NEWS_CATEGORY + " =? ", new String[] { tag, "0", "10" }, null, null, NewsDatabaseHelper.COLUMN_ID + " DESC " + " LIMIT ? , ? ");
	}
	
	public Cursor queryCountLocalNews(int column, String search) {
		Cursor c = null;
		switch(column){
		case 0:
			c = newsDatabase.query(NewsDatabaseHelper.TAB_NEWS, null, NewsDatabaseHelper.COLUMN_NEWS_TITLE + " like ? ", new String[] { "%" + search + "%" }, null, null, null);
		    break;
		case 1:
			c = newsDatabase.query(NewsDatabaseHelper.TAB_NEWS, null, NewsDatabaseHelper.COLUMN_NEWS_DESCRIPTION + " like ? ", new String[] { "%" + search + "%" }, null, null, null);
			break;
		case 2:
			c = newsDatabase.query(NewsDatabaseHelper.TAB_NEWS, null, NewsDatabaseHelper.COLUMN_NEWS_CONTENT + " like ? ", new String[] { "%" + search + "%" }, null, null, null);
			break;
		}
		return c;
	}
	
	public int queryCountLocalCategorys() {
		Cursor c = newsDatabase.query(NewsDatabaseHelper.TAB_CATEGORYS, null, null, null, null, null, null);
		int count = 0;
		if (c != null)
			count = c.getCount();
		c.close();
		return  count;
	}
	
	public Cursor queryLocalCategorys() {
		return  newsDatabase.query(NewsDatabaseHelper.TAB_CATEGORYS, null, null, null, null, null, NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_ID);
	}
	
	public boolean insertCountLocalCategorys(ContentValues values) {
		return newsDatabase.insert(NewsDatabaseHelper.TAB_CATEGORYS, null, values) > 0;
	}
	
	public boolean deleteCountLocalCategorys(String title, String url) {
		return newsDatabase.delete(NewsDatabaseHelper.TAB_CATEGORYS, NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_NAME + " =? "  + " AND " +  NewsDatabaseHelper.COLUMN_NEWS_CAGETORY_URL + " =? ",new String[] { title, url })>= 0;
	}
	
	
	public boolean insertCountLocalFavoritesNews(ContentValues values) {
		return newsDatabase.insert(NewsDatabaseHelper.TAB_FAVORITES_NEWS, null, values) > 0;
	}
	
	public Cursor queryCountLocalFavoritesNews() {
		return newsDatabase.query(NewsDatabaseHelper.TAB_FAVORITES_NEWS, null, null, null, null, null, null);
	}
	
	public boolean deleteCountLocalFavorites(long id) {
		return newsDatabase.delete(NewsDatabaseHelper.TAB_FAVORITES_NEWS, NewsDatabaseHelper.COLUMN_FAVORITES_ID + " =? " ,new String[] {String.valueOf(id)})>= 0;
	}
	
}
